package com.beiding.sqlbuilder.impl;

import com.beiding.sqlbuilder.*;

import java.util.List;

public class SimpleExpressionBuilder implements ExpressionBuilder {

    private ConditionUnitContainer conditionUnitContainer;

    private Tables tables = new Tables();

    void setConditionUnitContainer(ConditionUnitContainer conditionUnitContainer) {
        this.conditionUnitContainer = conditionUnitContainer;
    }

    ConditionUnitContainer getConditionUnitContainer() {
        return conditionUnitContainer;
    }

    @Override
    public String build(BuilderContext builderContext) {
        conditionUnitContainer.build(builderContext);
        ConditionUnitHolder conditionUnitHolder = ConditionUtils.buildConditionUnitHolder(conditionUnitContainer, builderContext.getConditionUnitHandlers());
        return conditionUnitHolder.execute();
    }

    public SimpleExpressionBuilder p(Object object) {
        conditionUnitContainer.addConditionUnit(new SimpleSingleParameter(ConditionUtils.createParameter(tables, object)));
        return this;
    }

    public SimpleExpressionBuilder plus() {
        symbol(OperationType.Plus);
        return this;
    }

    public SimpleExpressionBuilder gt() {
        symbol(OperationType.Gt);
        return this;
    }


    public SimpleExpressionBuilder lt() {
        symbol(OperationType.Lt);
        return this;
    }

    private void symbol(OperationType operationType) {
        conditionUnitContainer.addConditionUnit(new SimpleOperationConnector(operationType));
    }


    @Override
    public List<Table> getTables() {
        return tables.getTables();
    }


}
